/**
 * 首页
 */
var App = require("../libs/network/mvr.js");
var Imports = require("../libs/utils/imports.js");
var Template = require("../libs/plugins/art-template.js");
var XData = require("../libs/gamepump/xdata.js");
var NativeApp = require("../libs/phonegap/native-bridge.js");
var Url = require("../libs/utils/url.js");
var Cfg = require("../libs/base-conf.js");
var Stat = require("../libs/utils/stat.js");
var ButtonUI = require('../libs/ui/button-ui.js');

var conTpl = Imports("text!../page-layout/index/container.html");
var gameInfoTpl = Imports("text!../page-layout/index/gameinfo.html");
var gameInfoWaitTpl = Imports("text!../page-layout/index/gameinfo-wait.html");
var AdListTpl = Imports("text!../page-layout/index/adlist.html");
var guideTpl = Imports("text!../page-layout/index/guidelist.html");

var section = $("#dt-content");
section.append(conTpl);

var container = section.find("ul");
var gameInfoContainer = section.find('.dt-shitou');
var gameInfoDownload = section.find('.dt-btn');
var adListContainer = section.find('.dt-nav');
var btn = section.find('a.dt-btn');

//点击事件
container.on('tap', 'li a', function(e){
    var url = $(e.currentTarget).attr('data-url');
    console.log('go: ' + url);
    if(url){
        var newId= url.substring(url.indexOf("=")+1,url.length);
        Stat.addActionStat("detail_news","tj_lb",newId);
        Url.gotoPage(url);
        e.preventDefault();
        return false;
    }
});

adListContainer.on('tap', 'div span', function(e){
    var url = $(e.currentTarget).attr('data-url');
    console.log('go: ' + url);
    if(url){
        var newId= url.substring(url.indexOf("=")+1,url.length);
        Stat.addActionStat("detail_news","tj_gz",newId);
        Url.gotoPage(url);
        e.preventDefault();
        return false;
    }
});

var gameInfoWaitDlist = null;
var gameInfoDList = null;

var initGameInfo = function(){
    if(gameInfoWaitDlist!==null && gameInfoDList!==null){
        var dobj = gameInfoWaitDlist[0];
        var gameInfoState = dobj.orderId;
        console.log('gameInfoState:' + gameInfoState);
        console.log(gameInfoDList);

        if(gameInfoState === '1'){
            gameInfoContainer.append(Template(gameInfoWaitTpl)({"item": dobj}));
            gameInfoDownload.hide();
        }else{
            var gameInfo = XData.listSlice(gameInfoDList).glist[0];
            var pkgInfo = XData.extendPkginfo(gameInfoDList[0].game,gameInfoDList[0].gpkg);
            console.log(gameInfo, pkgInfo);

            gameInfoContainer.append(Template(gameInfoTpl)({"gameInfo": gameInfo}));
            gameInfoDownload.show();

            if(gameInfoState === '2' && pkgInfo){
                //该自由列表状态2（页面上写死游戏ID+按钮为下载或下载其他状态）
                //包装下载按钮,注册相应的事件
                new ButtonUI(gameInfo, pkgInfo, gameInfoDownload); 
            }else{
                //该自由列表状态3（页面上写死游戏ID+按钮为抢号+后台配置跳转到浏览器模式链接）
                gameInfoDownload.text('抢号').on('tap', function(){
                    //访问抢号的URL
                    console.log('go to: ' + dobj.navLogo);
                    Stat.addActionStat("btn_getgift", "", "");
                    Url.gotoPage(dobj.navLogo);
                });
            }
        }
    }
};

//顶部游戏信息
App.addModule('GameInfoWait',
    App.Model.extend({
        'key': "602",                         //数据协议keycode
        'private': 1,                         //是否走专属通道传递参数
        'params': "pcount=6&freeid=14"      //专属参数定义
    }),
    App.View.extend({
        //视图渲染回调函数定义
        render: function(dlist){
            gameInfoWaitDlist = dlist;
            initGameInfo();
        }
    }),
    true
);

//游戏头部信息
App.addModule('GameInfo',
    App.Model.extend({
        'key': "1",                            //数据协议keycode
        'private': 1,                         //是否走专属通道传递参数
        'params': "gid=" + Cfg.gameId
    }),
    App.View.extend({
        //视图渲染回调函数定义
        render: function(dlist){
            gameInfoDList = dlist;
            initGameInfo();
        }
    }),
    true
);

//广告栏位列表
App.addModule('AdList',
    App.Model.extend({
        'key' : "201", //数据协议keycode
        'private' : 1, //是否走专属通道传递参数
        'params' : "pcount=12&adposid=433&sort=time" //专属参数定义
    }),
    App.View.extend({
        //视图渲染回调函数定义
        render: function(dlist){
            var tpl = Template(AdListTpl);
            var dom = tpl({"dataList": dlist});
            adListContainer.append(dom);
        }
    }),
    true
);

//推荐攻略列表
App.addModule("GuideList",
    App.Model.extend({
        'key' : "201", //数据协议keycode
        'private' : 1, //是否走专属通道传递参数
        'params' : "pcount=12&adposid=434&sort=time" //专属参数定义
    }),
    App.View.extend({
        //容器标识
        container: container,

        //视图渲染回调函数定义
        render: function(dlist){
            var tpl = Template(guideTpl);
            var dom = tpl({"dataList": dlist});
            this.container.append(dom);
            window.slipjs.refresh();
        }
    })
);

App.Requester.init({
    container: container
});
App.Requester.tigger();